package com.iwdnb.gkgz.common.utils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUtil;
import com.iwdnb.gkgz.common.enums.RangeTypeEnums;
import com.iwdnb.gkgz.common.model.dto.StockDayData;
import com.iwdnb.gkgz.common.model.dto.StockRangeDTO;
import com.iwdnb.gkgz.common.quota.MovingAverage;
import org.apache.commons.collections4.CollectionUtils;

import static com.iwdnb.gkgz.common.utils.StockUtils.getAveragePrice;

public class StockTrendUtils {

    /**
     * 获取股票趋势
     *
     * @param stockDayData
     * @param period
     * @return
     */
    public static String getTrend(StockDayData stockDayData, List<StockDayData> stockDayDataList, int period) {
        List<StockDayData> trendDataList = StockUtils.getStockDayDataListBeforeDate(stockDayDataList,
            stockDayData.getDate(),
            50 + period);
        //MovingAverage.calculateEMA(trendDataList, period);
        trendDataList = StockUtils.getStockDayDataListBeforeDate(trendDataList, stockDayData.getDate(), 20);
        String key = period + "";
        BigDecimal minPrice = getAveragePrice(trendDataList.get(0), key);
        BigDecimal maxPrice = minPrice;
        int minIndex = 0;
        int maxIndex = 0;
        for (int i = 0; i < trendDataList.size(); i++) {
            StockDayData data = trendDataList.get(i);
            BigDecimal averagePrice = getAveragePrice(data, key);
            if (BigDecimalUtils.isGe(averagePrice, maxPrice)) {
                maxPrice = averagePrice;
                maxIndex = i;
            } else if (BigDecimalUtils.isLt(averagePrice, minPrice)) {
                minPrice = averagePrice;
                minIndex = i;
            }
        }
        BigDecimal closePrice = stockDayData.getClosePrice();
        BigDecimal range = BigDecimalUtils.subtract(maxPrice, minPrice);
        BigDecimal rate = BigDecimalUtils.divideToPrecent(range, minPrice);
        BigDecimal upRange = BigDecimalUtils.subtract(maxPrice, closePrice);
        BigDecimal upRate = BigDecimalUtils.divideToPrecent(upRange, closePrice);
        BigDecimal lowRange = BigDecimalUtils.subtract(closePrice, minPrice);
        BigDecimal lowRate = BigDecimalUtils.divideToPrecent(lowRange, closePrice);
        Date date = DateUtil.parseDate(stockDayData.getDate());
        List<StockRangeDTO> stockRangeDTOList = StockRangeUtils.calculateStockRange(stockDayDataList);
        stockRangeDTOList = stockRangeDTOList.stream()
            .filter(stockRangeDTO -> DateUtil.parseDate(stockRangeDTO.getEndDate()).before(date))
            .collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(stockRangeDTOList)) {
            StockRangeDTO stockRangeDTO = stockRangeDTOList.get(stockRangeDTOList.size() - 1);
            BigDecimal highPrice = stockRangeDTO.getHighPrice();
            BigDecimal highRange = BigDecimalUtils.subtract(highPrice, closePrice);
            BigDecimal highRate = BigDecimalUtils.divideToPrecent(highRange, closePrice);
            if (BigDecimalUtils.isGt(highRate, new BigDecimal(20))) {
                return RangeTypeEnums.DOWN.getCode();
            }
        }
        //5个点振幅之内,是横盘
        if (maxIndex > minIndex) {
            return RangeTypeEnums.UP.getCode();
        } else if (maxIndex < minIndex) {
            if (BigDecimalUtils.isLe(rate, new BigDecimal(1))) {
                return RangeTypeEnums.STABLE.getCode();
            }
            return RangeTypeEnums.DOWN.getCode();
        } else {
            return RangeTypeEnums.STABLE.getCode();
        }
    }


}

